package com.alpaca.recommend.action;


import com.alpaca.recommend.hbase.HTableClients;
import com.alpaca.recommend.hbase.init.HBaseTables;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.spring.Inject;
import com.jfinal.plugin.spring.IocInterceptor;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;

import java.io.IOException;

/**
 * Created by wangj on 2015/2/2.
 */
@Before(IocInterceptor. class)
public class HelloController extends Controller{

    @Inject.BY_TYPE
    private HTableClients hBaseClient;

    @Inject.BY_TYPE
    private HBaseTables hBaseTables;

    public void index() {
        renderText("Hello JFinal World.");
    }

    public void test() throws IOException {
        Table hTableInterface = hBaseClient.getHTable("items" );
        try {
            Put put = new Put("1".getBytes());
            put.add("i".getBytes() , "name".getBytes() , "10".getBytes());
            hTableInterface.put(put);
            renderText("table name is :" + hTableInterface.getName());
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            hTableInterface.close();
        }
    }

    public void init() throws IOException {
        hBaseTables.createTables();
        renderText("suc");
    }
}
